package com.ibeeking.found.cop.b.rest.service.mq;

import com.ibeeking.found.common.entity.MqMsgEventParam;
import com.ibeeking.found.cop.api.common.param.MsgEventDepartmentParam;
import com.ibeeking.found.cop.b.rest.service.ICopDepartmentService;
import com.ibeeking.found.cop.b.rest.service.ICopEmployeeService;
import com.ibeeking.found.cop.b.rest.service.IWxOpenService;
import com.ibeeking.found.cop.service.common.bo.CopEmployeeExtAttrBO;
import com.ibeeking.found.cop.service.common.bo.CopEmployeeExternalAttrBO;
import com.ibeeking.found.cop.service.common.bo.CopEmployeeWechatChannelBO;
import com.ibeeking.found.cop.service.common.param.CopDepartmentEmployeeParam;
import com.ibeeking.found.cop.service.common.param.CopDepartmentParam;
import com.ibeeking.found.cop.service.common.param.CopEmployeeParam;
import com.ibeeking.found.cop.service.common.param.MqSyncCopParam;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpTpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.constant.WxCpConsts;
import me.chanjar.weixin.cp.constant.WxCpTpConsts;
import me.chanjar.weixin.cp.tp.service.WxCpTpDepartmentService;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
import me.chanjar.weixin.cp.tp.service.WxCpTpUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CopContactMqMsgEventHandler {

    @Resource
    private ICopDepartmentService copDepartmentServiceImpl;

    @Resource
    private ICopEmployeeService copEmployeeServiceImpl;

    @Resource
    private IWxOpenService wxOpenServiceImpl;

    public boolean addDepartmentMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> createDepartmentMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String msgType = mqParam.getMsgType();
            String event = mqParam.getEvent();
            String changeType = mqParam.getChangeType();
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDepartmentParam departmentParam = JsonUtils.fromJson(paramStr, MsgEventDepartmentParam.class);

            return true;
        } catch (Exception e) {
            LogUtils.error("同步部门信息任务处理异常:{}", e);
            return false;
        }
    }

    public boolean updateDepartmentMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> updateDepartmentMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String msgType = mqParam.getMsgType();
            String event = mqParam.getEvent();
            String changeType = mqParam.getChangeType();
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDepartmentParam departmentParam = JsonUtils.fromJson(paramStr, MsgEventDepartmentParam.class);

            return true;
        } catch (Exception e) {
            LogUtils.error("同步部门信息任务处理异常:{}", e);
            return false;
        }
    }

    public boolean deleteDepartmentMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> deleteDepartmentMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String msgType = mqParam.getMsgType();
            String event = mqParam.getEvent();
            String changeType = mqParam.getChangeType();
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDepartmentParam departmentParam = JsonUtils.fromJson(paramStr, MsgEventDepartmentParam.class);

            return true;
        } catch (Exception e) {
            LogUtils.error("同步部门信息任务处理异常:{}", e);
            return false;
        }
    }

    public boolean addEmployeeMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> createEmployeeMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String msgType = mqParam.getMsgType();
            String event = mqParam.getEvent();
            String changeType = mqParam.getChangeType();
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDepartmentParam departmentParam = JsonUtils.fromJson(paramStr, MsgEventDepartmentParam.class);

            return true;
        } catch (Exception e) {
            LogUtils.error("同步部门信息任务处理异常:{}", e);
            return false;
        }
    }

    public boolean updateEmployeeMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> updateEmployeeMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String msgType = mqParam.getMsgType();
            String event = mqParam.getEvent();
            String changeType = mqParam.getChangeType();
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDepartmentParam departmentParam = JsonUtils.fromJson(paramStr, MsgEventDepartmentParam.class);

            return true;
        } catch (Exception e) {
            LogUtils.error("同步部门信息任务处理异常:{}", e);
            return false;
        }
    }

    public boolean deleteEmployeeMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> deleteEmployeeMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String msgType = mqParam.getMsgType();
            String event = mqParam.getEvent();
            String changeType = mqParam.getChangeType();
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDepartmentParam departmentParam = JsonUtils.fromJson(paramStr, MsgEventDepartmentParam.class);

            return true;
        } catch (Exception e) {
            LogUtils.error("同步部门信息任务处理异常:{}", e);
            return false;
        }
    }

}

